const byteOrderMarks = [
  {
    encoding: "UTF-EBCDIC",

    regex: new RegExp("221 115 102 115"),
  },

  {
    encoding: "GB-18030",

    regex: new RegExp("132 49 149 51"),
  },

  {
    encoding: "UTF-32LE",

    regex: new RegExp("255 254 0 0"),
  },

  {
    encoding: "UTF-32BE",

    regex: new RegExp("0 0 254 255"),
  },

  {
    encoding: "UTF-8",

    regex: new RegExp("239 187 191"),
  },

  {
    encoding: "UTF-7",

    regex: new RegExp("43 47 118"),
  },

  {
    encoding: "UTF-1",

    regex: new RegExp("247 100 76"),
  },

  {
    encoding: "SCSU",

    regex: new RegExp("14 254 255"),
  },

  {
    encoding: "BOCU-1",

    regex: new RegExp("251 238 40"),
  },

  {
    encoding: "UTF-16BE",

    regex: new RegExp("254 255"),
  },

  {
    encoding: "UTF-16LE",

    regex: new RegExp("255 254"),
  },
];

const checkByteOrderMark = (uInt8Start) => {
  for (const element of byteOrderMarks) {
    if (element.regex.test(uInt8Start)) return element.encoding;
  }

  return null;
};

const byteOrderMarkBuffer = new FileReader();

byteOrderMarkBuffer.onload = () => {
  const uInt8String = new Uint8Array(byteOrderMarkBuffer.result)
    .slice(0, 4)
    .join(" ");

  const byteOrderMark = checkByteOrderMark(uInt8String);

  if (byteOrderMark) {
    const byteOrderMarkReader = new FileReader();

    byteOrderMarkReader.onload = () => {
      data.content = byteOrderMarkReader.result;

      return data.content;
    };

    byteOrderMarkReader.onerror = (err) => {
      reject(err);
    };

    byteOrderMarkReader.readAsText(file, fileInfo.encoding);
  }
};

byteOrderMarkBuffer.readAsArrayBuffer(file);
